package com.qin.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.PmsProductCategory;
import com.qin.mall.service.PmsProductCategoryService;
import com.qin.mall.dto.PmsProductCategoryWithChildrenItem;
import com.qin.mall.dto.ProductCategoryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品分类Controller
 * @author Leisure
 * create 2022 2022/11/6 16:01
 **/
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryConteoller {
    @Autowired
    private PmsProductCategoryService productCategoryService;

    /**
     * 测试用例:
     * {
     *     "name":"电子产品",
     *     "navStatus": 1,
     *     "showStatus": 1
     * }
     * @param productCategoryParam
     * @return
     */
    // TODO 创建商品分类
    @PostMapping("create$")
    public CommonResult createCategory(@RequestBody ProductCategoryParam productCategoryParam){
        int count = productCategoryService.create(productCategoryParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 根据id修改商品 分类
    @PostMapping("update$/{id}")
    public CommonResult updateCategory(@PathVariable Long id,
                                 @RequestBody ProductCategoryParam productCategoryParam){
        int count = productCategoryService.update(id, productCategoryParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 测试用例:
     * parentId:1
     * pageNum:2
     * pageSize:2
     * @param parentId
     * @param pageSize
     * @param pageNum
     * @return
     */
    //TODO 分页获取商品分类
    @GetMapping("/list$/{parentId}")
    @ResponseBody
    public CommonResult<MyBatisPlusPage<PmsProductCategory>> getList(@PathVariable Long parentId,
                                                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ){
        Page<PmsProductCategory> list = productCategoryService.getList(parentId, pageNum, pageSize);
        return CommonResult.success(MyBatisPlusPage.mpPage(list));
    }

    //TODO 根据id删除商品分类
    @PostMapping("delete$/{id}")
    public CommonResult deleteById(@PathVariable Long id){
        int count = productCategoryService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 根据ID获取商品分类
    @GetMapping("$/{id}")
    public CommonResult<PmsProductCategory> selectById(@PathVariable Long id){
        PmsProductCategory category = productCategoryService.getItem(id);
        return CommonResult.success(category);
    }

    //TODO 批量修改导航状态
    @PostMapping("/update/navStatus$")
    public CommonResult updateNavStatus(@RequestParam("navStatus") Integer navStatus,
                                        @RequestParam("ids") List<Long> ids){
        int count = productCategoryService.updateNavStatus(ids, navStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 批量修改显示状态
    @PostMapping("/update/showStatus$")
    public CommonResult updateShowStatus(@RequestParam("showStatus") Integer showStatus,
                                         @RequestParam("ids") List<Long> ids){
        int count = productCategoryService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 以层级形式获取商品分类
    @GetMapping("/list/withChildren$")
    public CommonResult<List<PmsProductCategoryWithChildrenItem>> withChildren(){
        List<PmsProductCategoryWithChildrenItem> info = productCategoryService.listWithChildren();
        return CommonResult.success(info);
    }
}
